﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PMS.BusinessFacade;
using PMS.EntityManage;
using PMS.Common;
using PMS.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using EntityManage;
using BusinessFacade;
using System.IO;

public partial class Modules_Default_MyMessageAdd : System.Web.UI.Page
{
    IOther_MessageHandler msgHandler = new Other_MessageManage();
    IOther_MessageReceverHandler msgRecvHandler = new Other_MessageReceverManage();
    ISys_UserHandler userHandler = new Sys_UserManage();
    ISys_OrganizationHandler orgHandler = new Sys_OrganizationManage();
    ISys_OrganizationHandlerExt orgExtHandler = new Sys_OrganizationManageExt();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!CheckPurviews())
        {
            Response.Redirect("~/Login.aspx");
        }
        if (!IsPostBack)
        {
            int userId = int.Parse(Session["CurrentUserID"].ToString());
            // 当前用户所在单位的所有下级单位用户、当前单位用户、上级单位用户
            Sys_UserQuery userQuery = userHandler.GetSys_UserQuery(userId);
            if (userQuery != null)
            {
                Sys_OrganizationQuery orgQuery = orgHandler.GetSys_OrganizationQuery(userQuery.Org_ID);
                string orgIds = orgExtHandler.GetAllChildIdsByParentID(orgQuery.ID);
                orgIds = orgIds != null && orgIds.Length > 0 ? orgIds + "," + userQuery.Org_ID.ToString() : userQuery.ID.ToString();

                for (int i = 0; i < 20; i++)
                {
                    if (orgQuery.Parent_ID != null && orgQuery.Parent_ID != 0)
                    {
                        orgQuery = orgHandler.GetSys_OrganizationQuery(orgQuery.Parent_ID);
                        orgIds = orgIds + "," + orgQuery.ID.ToString();
                    }
                    else
                    {
                        break;
                    }
                }

                DataTable dtUser = userHandler.GetSys_UserList(" Org_ID in(" + orgIds + ")", null, " Name asc");
                chkUserList.DataSource = dtUser;
                chkUserList.DataTextField = "Name";
                chkUserList.DataValueField = "ID";
                chkUserList.DataBind();
            }
        }
    }

    private bool CheckPurviews()
    {
        if (Session["UserPurviews"] != null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        int userId = int.Parse(Session["CurrentUserID"].ToString());

        // 接收用户
        List<int> userIds = new List<int>();
        for (int i = 0; i < chkUserList.Items.Count; i++)
        {
            if (chkUserList.Items[i].Selected)
            {
                userIds.Add(int.Parse(chkUserList.Items[i].Value));
            }
        }

        if (userIds.Count <= 0)
        {
            Jscript.Alert("请选择接收人", this.Page);
            return;
        }

        Other_MessageQuery msgQuery = new Other_MessageQuery();
        msgQuery.Title = txtTitle.Text;
        msgQuery.User_ID = userId;
        msgQuery.Content = txtContent.Text;
        msgQuery.Create_Time = DateTime.Now;

        int msgId = 0;
        msgHandler.InsertOther_Message(msgQuery, ref msgId);

        // 添加附件
        if (msgId != 0)
        {
            foreach (int uid in userIds)
            {
                Other_MessageReceverQuery recvQuery = new Other_MessageReceverQuery();
                recvQuery.Message_ID = msgId;
                recvQuery.User_ID = uid;
                recvQuery.Status = 0;

                msgRecvHandler.InsertOther_MessageRecever(recvQuery);
            }
        }

        Jscript.Alert("添加成功");
        Jscript.ReturnAndClose(this.Page, "true");
    }

}
